{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu\n",
    "!pip3 install tokenizers -U\n",
    "!pip3 install transformers -U\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch  \n",
    "from transformers import AutoTokenizer, AutoModelForCausalLM  \n",
    "import jsonlines"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load the tokenizer and model from Hugging Face  \n",
    " \n",
    "model_id = \"meta-llama/Meta-Llama-3-8B\"\n",
    "\n",
    "tokenizer = AutoTokenizer.from_pretrained(model_id)\n",
    "model = AutoModelForCausalLM.from_pretrained(\n",
    "    model_id,\n",
    "    torch_dtype=torch.float32,\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Save to disk  \n",
    "output_path = \"golden_data_llama3-8b.jsonl\"   \n",
    "    \n",
    "        \n",
    "# Your prompt text  \n",
    "prompt_texts = [\"I love to\"]\n",
    "all_data_to_save = []\n",
    "\n",
    "\n",
    "for prompt_text in prompt_texts:\n",
    "    # Encode the prompt text  \n",
    "    input_ids = tokenizer.encode(prompt_text, return_tensors='pt')  \n",
    "\n",
    "    # Get the logits for the prompt + completion  \n",
    "    with torch.no_grad():  \n",
    "        outputs = model(input_ids)\n",
    "        logits = outputs.logits  \n",
    "\n",
    "        # Convert logits to fp32  \n",
    "        logits = logits.cpu().numpy().astype('float32')  \n",
    "\n",
    "        # Prepare data to be saved  \n",
    "        data_to_save = {  \n",
    "            \"prompt\": prompt_text,  \n",
    "            \"tokens\": input_ids.tolist()[0],  \n",
    "            \"logits\": logits.tolist()[0]  # Convert numpy array to list for JSON serialization  \n",
    "        }  \n",
    "        all_data_to_save.append(data_to_save)\n",
    "    \n",
    "with jsonlines.open(output_path,'w') as f:    \n",
    "    f.write_all(all_data_to_save)\n",
    "\n",
    "\n",
    "\n",
    "print(f\"Data saved to {output_path}\")  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
